// TODO: Verify dependencies
// NOTE: Make sure that t

(function ($) {
    $.fn.jollaback = function (sFormDom, fCallback, oOptions, oDialogOptions, oAjaxSubmitOptions) {
        // Default Jollaback settings
        var oSettings = {
            feedbackDomClass : 'jollaback',
            handleEvent      : 'click'
        };
        // Default Dialog settings
        var oDialogSettings = {
            autoOpen  : false,
            draggable : false,
            modal     : true,
            resizable : false,
            title     : 'Feedback'
        }
        // Default ajaxSubmit settings
        var oAjaxSubmitSettings = {
            target : jForm
        }
        // Merge user and default settings
        if (!oOptions) oOptions = {};
        if (!oDialogOptions) oDialogOptions = {};
        if (!oAjaxSubmitOptions) oAjaxSubmitOptions = {};
        $.extend(oSettings, oOptions);
        $.extend(oDialogSettings, oDialogOptions);
        $.extend(oAjaxSubmitSettings, oAjaxSubmitOptions);

        // Variables
        var jForm = $('#' + sFormDom);
        var eCurrentElement = null;
        var jHighlight = $('div#jollaback_highlight');
        var bJollabackEventsActive = false;
        var jJollabackDom = $('.' + oSettings.feedbackDomClass);
       
        bindJollabackEvents();                 // Set up events for contextual overlays
        jForm.dialog(oDialogSettings);         // Attach dialog to form
        jForm.ajaxSubmit(oAjaxSubmitSettings); // Make the form submit via Ajax
        jForm.find('#start_user_feedback_interaction').click(handleInteractionEvent);
        // Apply event handler to each interacted feedback element
        this.each(function () {
            eval('$(this).' + oSettings.handleEvent + '(handleFeedbackEvent);');
        });
        
        /**
         * Handles the event specified for feedback.
         */
        function handleFeedbackEvent()
        {
            jForm.dialog('open'); // DEPENDENCY Open the form in a jQuery UI dialog
            $(this).hide();
        }

        /**
         * Handles the interaction feedback event
         */
        function handleInteractionEvent()
        {
            bJollabackEventsActive = true;
            jForm.dialog('close');
        }

        /**
         * Binds key events that interact with the page when active
         */
        function bindJollabackEvents()
        {
            jJollabackDom.bind('mouseover.jollaback', function(eEvent) {
                if (bJollabackEventsActive && (this != eCurrentElement))
                {
                    eEvent.stopPropagation(); // NO BUBBLE
                    eCurrentElement = this;
                    jHighlight.css('width', this.offsetWidth + 'px');
                    jHighlight.css('height', this.offsetHeight + 'px');
                    jHighlight.css('top', this.offsetTop + 'px');
                    jHighlight.css('left', this.offsetLeft + 'px');
                    jHighlight.show();
                }
            });
            jHighlight.bind('click.jollaback', function(eEvent) {
                if (bJollabackEventsActive)
                {
                    eEvent.stopPropagation(); // NO BUBBLE
                    eCurrentElement = null;
                    jHighlight.hide();
                    bJollabackEventsActive = false; // Race condition? Might be better to unbind events and then 
                    jForm.dialog('open');
                }
            });
        }

        return this; // import to allowing chaining in jQuery calls
    }
})(jQuery);
